package com.xique.door.wechat.service;

import cn.hutool.core.collection.CollUtil;
import com.xique.door.bean.domain.WxAutoReply;
import com.xique.door.service.wechat.IWxAutoReplyService;
import com.xique.door.wechat.domain.Image;
import com.xique.door.wechat.domain.ImageMessage;
import com.xique.door.wechat.domain.TextMessage;
import com.xique.door.wechat.util.WechatMessageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @program: xq-cloud
 * @ClassName TextReplyService
 * @description:
 * @author: HdongSun
 * @create: 2022-10-26 15:43
 **/
@Service
@Slf4j
@RequiredArgsConstructor
public class TextReplyService {

    private static final String FROM_USER_NAME = "FromUserName";
    private static final String TO_USER_NAME = "ToUserName";
    private static final String CONTENT = "Content";

    private final IWxAutoReplyService wxAutoReplyService;

    /**
     * 自动回复文本内容
     *
     * @param requestMap
     * @return
     */
    public String reply(Map<String, String> requestMap) {
        String wechatId = requestMap.get(FROM_USER_NAME);
        String gongzhonghaoId = requestMap.get(TO_USER_NAME);

        TextMessage textMessage = WechatMessageUtil.getDefaultTextMessage(wechatId, gongzhonghaoId);

        String content = requestMap.get(CONTENT);
        if (content == null) {
            List<WxAutoReply> defaultList = wxAutoReplyService.selectListByKeyword("默认");
            WxAutoReply reply = defaultList.get(0);
            textMessage.setContent(reply.getContent());
        } else {
            // 自动回复处理
            List<WxAutoReply> list = wxAutoReplyService.selectListByKeyword(content);
            if (CollUtil.isEmpty(list)) {
                List<WxAutoReply> defaultList = wxAutoReplyService.selectListByKeyword("默认");
                WxAutoReply reply = defaultList.get(0);
                textMessage.setContent(reply.getContent());
            } else {
                WxAutoReply reply = list.get(0);
                if (reply.getType().equals(WechatMessageUtil.RESP_MESSAGE_TYPE_TEXT)) {
                    textMessage.setContent(reply.getContent());
                }
                if (reply.getType().equals(WechatMessageUtil.RESP_MESSAGE_TYPE_IMAGE)) {
                    Image image = new Image();
                    image.setMediaId(reply.getContent());
                    ImageMessage imageMessage = new ImageMessage();
                    imageMessage.setToUserName(wechatId);
                    imageMessage.setFromUserName(gongzhonghaoId);
                    imageMessage.setCreateTime(System.currentTimeMillis() / 1000);
                    imageMessage.setMsgType(WechatMessageUtil.RESP_MESSAGE_TYPE_IMAGE);
                    imageMessage.setImage(image);
                    // 将图片消息对象转换成xml
                    return WechatMessageUtil.messageToXml(imageMessage);
                }
            }
        }

        return WechatMessageUtil.messageToXml(textMessage);
    }
}
